<%@page language="java" pageEncoding="UTF-8" contentType="text/html;charset=utf-8"%>
<%@include file="/includes/taglibs.jsp"%>
<%@page import="com.feib.soeasy.util.UserUtil"%>
<%@page import="com.feib.soeasy.util.CodeTableUtil"%>
<%@page import="com.feib.soeasy.model.Payment"%>
<script type="text/javascript"><!--
		
	$(document).ready(function() {	
		$("[id^='saveCommissionBtn']").click(function() {
			$('#actionType').val('S');
			var errMsg = "";
			
			
			$("[id^='tr-header-']").each(function(i){
				var idx = this.id.substring(10);
				var nextId = parseInt(idx, 10) + 1;
				var commissionCodeId = '#commissionCodeTables\\['+idx+'\\]\\.commissionCode';
				var commissionCode = $(commissionCodeId).val();	
				var commissionCodeEmpty = (null == commissionCode || 0 == commissionCode.length);
				
				var commissionCodeNameId = '#commissionCodeTables\\['+idx+'\\]\\.commissionCodeName';
				var commissionCodeName = $(commissionCodeNameId).val();
				var commissionCodeNameEmpty = (null == commissionCodeName || 0 == commissionCodeName.length);
			
				var amountScopeSId = '#commissionCodeTables\\['+idx+'\\]\\.amountScopeS';
				var amountScopeS = $(amountScopeSId).val();
				var amountScopeSEmpty = (null == amountScopeS || 0 == amountScopeS.length);
				
				var amountScopeEId = '#commissionCodeTables\\['+idx+'\\]\\.amountScopeE';
				var amountScopeE = $(amountScopeEId).val();
				var amountScopeEEmpty = (null == amountScopeE || 0 == amountScopeE.length);
			
				var commissionTypeId = '#commissionCodeTables\\['+idx+'\\]\\.commissionType';
				var commissionType = $(commissionTypeId).val();
				var commissionTypeEmpty = (null == commissionType || 0 == commissionType.length);
				
				var commissionAmountId = '#commissionCodeTables\\['+idx+'\\]\\.commissionAmount';
				var commissionAmount = $(commissionAmountId).val();

				var groupAmountId = '#commissionCodeTables\\['+idx+'\\]\\.groupAmount';
				var groupAmount = $(groupAmountId).val();
				
				var bankAmountId = '#commissionCodeTables\\['+idx+'\\]\\.bankAmount';
				var bankAmount = $(bankAmountId).val();
				
				var payerAmountId = '#commissionCodeTables\\['+idx+'\\]\\.payerAmount';
				var payerAmount = $(payerAmountId).val();
				
				if (commissionCodeEmpty ){
					errMsg = errMsg + "序號["+ nextId + "]代碼 是必要欄位<br/>";
				}
				if (commissionCodeNameEmpty ){
					errMsg = errMsg + "序號["+ nextId + "]代碼名稱 是必要欄位<br/>";
				}
				if ( amountScopeEEmpty && amountScopeSEmpty){
					errMsg = errMsg + "序號["+ nextId + "]金額區間請輸入完整起迄<br/>";
				}
				else if (amountScopeSEmpty && !amountScopeEEmpty){
					errMsg = errMsg + "序號["+ nextId + "]請輸入完整金額區間(起)<br/>";
				}
				else if (!amountScopeSEmpty && amountScopeEEmpty){
					errMsg = errMsg + "序號["+ nextId + "]請輸入完整金額區間(迄)<br/>";					
				}
				else{
					var tmp1 = parseInt(amountScopeS, 10);
					
					if (isNaN(tmp1)){
						errMsg = errMsg + "序號["+ nextId + "]金額區間(起)值須為正整數<br/>";
					}
					else if(ValidateNumber(amountScopeS)){
						errMsg = errMsg + "序號["+ nextId + "]金額區間(起)值須為正整數<br/>";
					}
					else{	
						amountScopeS = tmp1;
					}   
					
					var tmp2 = parseInt(amountScopeE, 10);
					if (isNaN(tmp2)){
						errMsg = errMsg + "序號["+ nextId + "]金額區間(迄)值須為正整數<br/>";
					}
					else if(ValidateNumber(amountScopeE)){
						errMsg = errMsg + "序號["+ nextId + "]金額區間(迄)值須為正整數<br/>";
					}
					else{
						amountScopeE = tmp2;
					}
						
					
					if (!isNaN(tmp1) && !isNaN(tmp2)){
						if (amountScopeE - amountScopeS < 0)
							errMsg = errMsg + "序號["+ nextId + "]金額區間起迄錯誤<br/>";
					}
				}		
				
				if (commissionTypeEmpty ){
					errMsg = errMsg + "序號["+ nextId + "]手續費類別 是必要欄位<br/>";
				}
				
				if (commissionType=='I' &&  0 < payerAmount ){
					errMsg = errMsg + "序號["+ nextId + "]手續費類別為內含時,繳款人不須繳款者負擔,已更正為0<br/>";
					$(payerAmountId).val('0');
				}
				
				
				if (null == commissionAmount || 0 == commissionAmount.length){
					errMsg = errMsg + "序號["+ nextId + "]超商成本是必要欄位<br/>";
				}else{
					var tmp1 = parseInt(commissionAmount, 10);
					
					if (isNaN(tmp1)){
						errMsg = errMsg + "序號["+ nextId + "]超商成本值須為正整數<br/>";
					}
					else if(ValidateNumber(commissionAmount)){
						errMsg = errMsg + "序號["+ nextId + "]超商成本值須為正整數<br/>";
					}
										
				}			
	
				if (null == groupAmount || 0 == groupAmount.length){
					errMsg = errMsg + "序號["+ nextId + "]業者負擔是必要欄位<br/>";
				}else{
					var tmp1 = parseInt(groupAmount, 10);
					
					if (isNaN(tmp1)){
						errMsg = errMsg + "序號["+ nextId + "]業者負擔值須為正整數<br/>";
					}
					else if(ValidateNumber(groupAmount)){
						errMsg = errMsg + "序號["+ nextId + "]業者負擔值須為正整數<br/>";
					}
										
				}	
				if (null == bankAmount || 0 == bankAmount.length){
					errMsg = errMsg + "序號["+ nextId + "]帳務處理費是必要欄位<br/>";
				}else{
					var tmp1 = parseInt(bankAmount, 10);
					
					if (isNaN(tmp1)){
						errMsg = errMsg + "序號["+ nextId + "]帳務處理費值須為正整數<br/>";
					}
					else if(ValidateNumber(bankAmount)){
						errMsg = errMsg + "序號["+ nextId + "]帳務處理費值須為正整數<br/>";
					}
										
				}	
		
				
		     	if (null == payerAmount || 0 == payerAmount.length){
					errMsg = errMsg + "序號["+ nextId + "]繳款者負擔 是必要欄位<br/>";
				}else{
					var tmp1 = parseInt(payerAmount, 10);
					
					if (isNaN(tmp1)){
						errMsg = errMsg + "序號["+ nextId + "]繳款者負擔值須為正整數<br/>";
					}
					else if(ValidateNumber(payerAmount)){
						errMsg = errMsg + "序號["+ nextId + "]繳款者負擔值須為正整數<br/>";
					}
										
				}	
			   
			 
				if (parseInt(payerAmount, 10) > parseInt(commissionAmount, 10)){
					errMsg = errMsg + "序號["+ nextId + "]繳款者負擔不得大於超商成本<br/>";
				}
				 //2011/05/13 jeff Gu  start  電金部2011/05/09報表邏輯異動  增加超商成本檢核
				if (commissionType=='I' &&  parseInt(groupAmount, 10)  != parseInt(commissionAmount, 10)){
					errMsg = errMsg + "序號["+ nextId + "]手續費為內含時,超商成本為業者負擔<br/>";					
				}
				
				if (commissionType=='O' &&  ( (parseInt(payerAmount, 10)+ parseInt(groupAmount, 10) ) != parseInt(commissionAmount, 10))){
					errMsg = errMsg + "序號["+ nextId + "]手續費為外加時,超商成本為繳款者負擔+業者負擔 <br/>";					
				}
				  //2011/05/13 jeff Gu  end  			
					
				
			});
			if (0 != errMsg.length){
				$("#ajaxErrorMessageDiv").html(errMsg);
				$("#ajaxErrorMessageDiv" ).dialog({  
					title:'請修正下列錯誤' 
					,width: 400
					, resizable: false
					, modal: true
					, buttons: {
						Ok: function() {
							$( this ).dialog( "close" );
						}
					}
				});
				return false;
			}		
			doModifyCommissionAction();
		});	

	
			
		function ValidateNumber(pnumber)
		{
		    if (!/^\d+$/.test(pnumber))
		    {
		      
		      return true;
		    }
		    return false;
		}
		
		
		
		$("[id^='addCommissionBtn']").click(function() {
			$('#actionType').val('A');
			
			doModifyCommissionAction();
		});	
		<c:if test="${null != actionBean.modifyMessage}">
		$("#ajaxErrorMessageDiv").html('${actionBean.modifyMessage}');
		$("#ajaxErrorMessageDiv" ).dialog({  
			title:'<%=UserUtil.getCurrentMenuItemName() %>' 
			,width: 600
			, resizable: false
			, modal: true
			, buttons: {
				Ok: function() {
					$( this ).dialog( "close" );
				}
			}
		});
	</c:if>
	});	
	//
--></script>

	<stripes:form name="confirmHiddenFrom" id="confirmHiddenFrom" beanclass="com.feib.soeasy.action.Soez9005ActionBean" 
		method="post" onsubmit="javascript: return false;">	
		<table id="listTable">
			<thead>
				<tr>
					<th>序號</th>
					<th>代碼</th>
					<th>代碼名稱</th>
					<th>金額區間(起 )</th>
					<th>金額區間(迄)</th>
					<th>手續費類別</th>
					<th>超商成本</th>
					<th>業者負擔 </th>				
					<th>繳款者負擔</th>
				</tr>
			</thead>
			<tbody>
			<c:if test="${null != actionBean.queryCommissionCodeResult && not empty actionBean.queryCommissionCodeResult.paginatedList}">
				<c:forEach items="${actionBean.queryCommissionCodeResult.paginatedList}" var="viewObj" varStatus="rowstat">
				<tr id="tr-header-${rowstat.index}">
					<td>${rowstat.index+1}
						<input type="hidden"
							name="commissionCodeTables[${rowstat.index}].commissionCodePk" 
							id="commissionCodeTables[${rowstat.index}].commissionCodePk" 
							value="<c:out value='${viewObj.commissionCodePk}'/>"/>
					</td>
					<td>
						<input type="text" size="5"
							name="commissionCodeTables[${rowstat.index}].commissionCode" 
							id="commissionCodeTables[${rowstat.index}].commissionCode" 
							value="<c:out value='${viewObj.commissionCode}'/>"
							maxlength="3"/>
					</td>
					<td>
						<input type="text" size="7" maxlength="20"
							name="commissionCodeTables[${rowstat.index}].commissionCodeName" 
							id="commissionCodeTables[${rowstat.index}].commissionCodeName" 
							value="<c:out value='${viewObj.commissionCodeName}'/>"/>
					</td>
					<td>
						<input type="text" size="7" maxlength="7"
							name="commissionCodeTables[${rowstat.index}].amountScopeS" 
							id="commissionCodeTables[${rowstat.index}].amountScopeS" 
							value="<c:out value='${viewObj.amountScopeS}'/>"/>
					</td>
					<td>
						<input type="text" size="7" maxlength="7"
							name="commissionCodeTables[${rowstat.index}].amountScopeE" 
							id="commissionCodeTables[${rowstat.index}].amountScopeE" 
							value="<c:out value='${viewObj.amountScopeE}'/>"/>
					</td>
					<td>
						<select
							id="commissionCodeTables[${rowstat.index}].commissionType" 
							name="commissionCodeTables[${rowstat.index}].commissionType" >
								<c:if test="${viewObj.commissionType == 'I' || viewObj.commissionType == null}">
									<option value="I" selected="selected">內含</option>
									<option value="O">外加</option>
								</c:if>
								<c:if test="${viewObj.commissionType == 'O'}">
									<option value="I">內含</option>
									<option value="O" selected="selected">外加</option>
								</c:if>

						</select>
					</td> 	
					<td>
						<input type="text" size="5" maxlength="6"
							name="commissionCodeTables[${rowstat.index}].commissionAmount" 
							id="commissionCodeTables[${rowstat.index}].commissionAmount" 
							value="<c:out value='${viewObj.commissionAmount}'/>"/>
					</td>
					<td><!--業者負擔-->
						<input type="text" size="5" maxlength="6"
							name="commissionCodeTables[${rowstat.index}].groupAmount" 
							id="commissionCodeTables[${rowstat.index}].groupAmount" 
							value="<c:out value='${viewObj.groupAmount}'/>"/>
						<input type="hidden" size="5" maxlength="6"
							name="commissionCodeTables[${rowstat.index}].bankAmount" 
							id="commissionCodeTables[${rowstat.index}].bankAmount" 
							value="<c:out value='0'/>"/>					
					</td>
					<td>
					
					<c:if test="${viewObj.commissionType == 'I' || viewObj.commissionType == null}">
									<input type="text" size="5" maxlength="6"
							name="commissionCodeTables[${rowstat.index}].payerAmount" 
							id="commissionCodeTables[${rowstat.index}].payerAmount" 														
							value="<c:out value='0'/>"   />
				   </c:if>
				 <c:if test="${viewObj.commissionType == 'O'}">
									<input type="text" size="5" maxlength="6"
							name="commissionCodeTables[${rowstat.index}].payerAmount" 
							id="commissionCodeTables[${rowstat.index}].payerAmount" 														
							value="<c:out value='${viewObj.payerAmount}'/>"/>
				</c:if>
							
						
					</td>			
				</tr>
				</c:forEach>
				</c:if>
			</tbody>
		</table>
		<table id="queryForm">
			<tr>
				
				<td class="queryFunction">
				<c:if test="${null != actionBean.queryCommissionCodeResult && not empty actionBean.queryCommissionCodeResult.paginatedList}">
					<input type="button" 
						id="saveCommissionBtn" name="saveCommissionBtn" 
						value="確認">
				</c:if>
					<input type="button" 
						id="addCommissionBtn" name="addCommissionBtn" 
						value="新增">
				</td>
			</tr>
		</table>
	<input type="hidden" id="method" name="doAjaxModCommissionAction"/>
	<input type="hidden" id="actionType" name="actionType"/>
	<stripes:hidden id="hidden2.qryCode" name="qryCode"/>
	<stripes:hidden id="hidden2.qryCodeName" name="qryCodeName"/>

	</stripes:form>
	
	
	<c:if test="${null == actionBean.queryCommissionCodeResult || empty actionBean.queryCommissionCodeResult.paginatedList}">
		<div id="queryNothingBlockDiv" style="display: none;">查無資料</div>
		<script type="text/javascript">
<!--
	$(document).ready(function() {
		$("#queryNothingBlockDiv").dialog("destroy");
		$("#queryNothingBlockDiv").dialog({
			title : '<%=UserUtil.getCurrentMenuItemName() %>查詢結果',
			width : 300,
			resizable : false,
			modal : true,
			buttons : {
				Ok : function() {
					$(this).dialog("close");
				}
			}
		});
	});
//-->
</script>
		
	</c:if>

